package com.magina.nety.config;

import com.alibaba.fastjson.JSON;
import com.magina.nety.redisCache.SearchModel;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/cache")
public class TestCacheController {

@Autowired
private TestCacheService testCacheService;

@Autowired
private RedissonClient reportRedisson;

    @GetMapping("/1/{id}")
    public Object testMethod1(@PathVariable("id")String id){
        System.out.println(id);
        List<String> list = testCacheService.findAll(id);
//        SimpleCacheManager simpleCacheManager = (SimpleCacheManager) SpringContextUtil.getBean("simpleCacheManager");
//        Object object = simpleCacheManager.getCache("test001");
//        if(null == object){
//            return "sa";
//        }
        return list;
    }

    @GetMapping("/01/{id}")
    public Object testMethod01(@PathVariable("id")String id){
        System.out.println(id);
        PageRequest pageRequest =  PageRequest.of(1,10,Sort.Direction.ASC,"name","code");
        PageRequest pageRequest1 =  PageRequest.of(1,12,Sort.Direction.DESC,"name");
        System.out.println(JSON.toJSONString(pageRequest));
        SearchModel searchModel = new SearchModel();
        ((SearchModel) searchModel).setCode("asd1");
        searchModel.setName("qwe1");
        Page<TestDemo> list = testCacheService.testPageRedis(searchModel,pageRequest);
        return list;
    }

    @GetMapping("/2")
    public Object testMethod2(){
        SimpleCacheManager simpleCacheManager = (SimpleCacheManager) SpringContextUtil.getBean("simpleCacheManager");
        Cache mycache = simpleCacheManager.getCache("mycache");
//        mycache.get()
//        if(null == object){
//            return "sa";
//        }
        Object object = mycache.get("asd");
        System.out.println("wqe");
        object = null == object ? "asd" : object;
        return object;
    }

    @GetMapping("/3/{key}")
    public Object testMethod3(@PathVariable("key") String key){
        RMap rMap=reportRedisson.getMap("test_map");
        rMap.put(key,"qwe");
        rMap.expire(5000L,TimeUnit.MILLISECONDS);
        return rMap;
    }

    @PostMapping("/testPost")
    public Object testMethod4(@RequestBody RequestTestVo requestTestVo){
        log.info(requestTestVo.getAk());
        log.info(requestTestVo.getCallee());
        log.info(requestTestVo.getCaller());
        log.info(requestTestVo.getSign());
        return requestTestVo;
    }

    private static final String preStr = "curl  -w \"%{time_total}\" -X POST \"http://www.bsats.cn/forbid.php?ak=2f9496e711b19f2f7e36910e4e6bf841&callId=";

    @PostMapping("/curl")
    public Object getCurlTimeout(@RequestBody List<String> phoneList ) throws Exception {
        List<String> result = Lists.newArrayList();
       phoneList.forEach(phone -> {
           String callId = UUID.randomUUID().toString();
        StringBuffer stringBuffer = new StringBuffer(preStr);
        String codeStr ="2f9496e711b19f2f7e36910e4e6bf841" + callId +"90f9f5682fa729c8b117526cdaae06d1";
           String encodeStr = null;
           try {
               encodeStr = md5(codeStr);
           } catch (Exception e) {
               e.printStackTrace();
           }
           stringBuffer.append(callId)
                .append("&caller=(0512)87660033&callee=")
                .append(phone)
                .append("&sign=")
                .append(encodeStr)
           .append("\"");
           result.add(stringBuffer.toString());
           System.out.println(stringBuffer.toString());
       });


    return result;
    }


    /**
     * MD5方法
     *
     * @return 密文
     * @throws Exception
     */
    public static String md5(String Str) throws Exception {
        //加密后的字符串
        System.out.println("MD5加密前的字符串为:encodeStr="+Str);
        String encodeStr=DigestUtils.md5DigestAsHex(Str.getBytes());
        System.out.println("MD5加密后的字符串为:encodeStr="+encodeStr);
        return encodeStr;
    }

    public static void main(String[] args) {
        List<Double> doubles = Arrays.asList(0.086307,
                0.075495,
                0.110609,
                0.074278,
                0.078910,
                0.084988,
                0.070741,
                0.064561,
                0.088411,
                0.065953,
                0.063831,
                0.068374);
        Double avg = doubles.stream().mapToDouble((x)->x).summaryStatistics().getAverage();
        System.out.println(avg);
    }



}
